﻿@model ThaireInvestmentWeb.Models.LoanInterestRateModel
@{
    ViewBag.Title = "LoanInterest";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script>
    $(document).ready(function () {

        $("#NewMLRRateDate").datepicker(
            {
                dateFormat: 'dd/mm/yy',
                yearRange: "-30:+40",
                showOn: 'button',
                showAnim: 'fadeIn',
                changeMonth: true,
                changeYear: true,
                showButtonPanel: true,
                buttonImage: '@Url.Content("~/Content/images/Calendar.png")'
            });
        
       
        $("#CancelBtn").click(function (event)
        {
           
            showConfirm(null, CancelBtnAction);
            return false;
        });

        $( "#NewMLR" ).change(function() {
            calInterestRate();
            FormatNumberFreeDigitsWithParam($(this));
        });

        $("#NewMLRMinus" ).change(function() {
            calInterestRate();
            FormatNumberFreeDigitsWithParam($(this));
        });
        
        $("#Installment").change(FormatNumber2Digits);

    });

        function calInterestRate()
        {
            $("#NewInterestRate" ).val(parseDouble($("#NewMLR" ).val())-parseDouble($("#NewMLRMinus" ).val()));
            if ($("#NewMLR" ).val() != '' && $("#NewMLRMinus" ).val() != '') {
                var newInterestRate = parseDouble($("#NewMLR" ).val())-parseDouble($("#NewMLRMinus" ).val());
                $('#NewInterestRate').val(newInterestRate);
                getInstallment(newInterestRate); 
            }

        }

        function CancelBtnAction(event)
        {     
            window.location.href= 'EditLoanHeader?headerId=@Model.HeaderID';
        }
        function AmortizeLoanSchedule(detailID,headerID)
        {
            // alert('back');
            window.location.href= 'AmortizeLoanSchedule?interestDetailID='+detailID+'&headerID='+headerID;
        }
        function getInstallment(value)
        {
            $.ajax({
                url: "@Url.Action("GetInstallment", "Loan")" + "?newInterestRate=" + value,
                type: 'POST',
                success: function(data) {
                    $('#Installment').val(data.Installment);
                    return false;
                }
            });
        }    
</script>

<p>
    <table class="navi" width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
            <td style="width: 100%;">
                <div class="navi">
                    Instrument Type > <a href="@Url.Action("ManageLoan", "Loan", new { headerId = Model.HeaderID })">Loans</a> > <a href="@Url.Action("EditLoanHeader", "Loan", new { headerId = Model.HeaderID })">Loans Header</a> > Interest
                </div>

            </td>
        </tr>
    </table>
    <table class="tableheader" width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
            <td height="38" align="center" style="width: 100%; text-align: center;">Interest 
                Rate</td>
        </tr>
    </table>
    @using (Html.BeginForm("LoanInterest", "Loan", FormMethod.Post, new { id = "mainForm" }))
    {
        <table width="951" border="0" align="center">
            <tr>
                <td>
                    <table class="tableform" width="100%" border="5" align="center" cellspacing="2">
                        <tr>
                            <td width="212">
                                <div align="right">ชื่อบริษัท: </div>
                            </td>
                            <td style="width: 4px">&nbsp;</td>
                            <td style="width: 236px">

                                @Html.TextBoxFor(model => model.CompanyName, new { @readonly = "readonly", style = "width: 250px;" })
                            </td>
                            <td width="160" align="right">
                                <div align="right">Maturity Status:</div>
                            </td>
                            <td style="width: 4px">&nbsp;</td>
                            <td width="208">
                                @Html.TextBoxFor(model => model.MaturityStatus, new { @readonly = "readonly", style = "width: 120px;" })
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <div align="right">เลขที่สัญญาเงินกู้: </div>
                            </td>
                            <td style="width: 4px">&nbsp;</td>
                            <td style="width: 236px">
                                @Html.TextBoxFor(model => model.ContractNumber, new { @readonly = "readonly", style = "width: 120px;" })
                            </td>
                            <td>
                                <div align="right">ชื่อผู้กู้: </div>
                            </td>
                            <td>&nbsp;</td>
                            <td>@Html.TextBoxFor(model => model.BorrowerName, new { @readonly = "readonly", style = "width: 200px;" }) </td>
                        </tr>
                        <tr>
                            <td>
                                <div align="right">อายุสัญญา(เริ่ม): </div>
                            </td>
                            <td style="width: 4px">&nbsp;</td>
                            <td style="width: 236px">@Html.TextBoxFor(model => model.ContractStartDate, new { @readonly = "readonly", style = "width: 120px;" }) </td>
                            <td>
                                <div align="right">อายุสัญญา(สิ้นสุด): </div>
                            </td>
                            <td>&nbsp;</td>
                            <td>@Html.TextBoxFor(model => model.ContractEndDate, new { @readonly = "readonly", style = "width: 120px;" }) </td>
                        </tr>
                        <tr>
                            <td>
                                <div align="right">จำนวนเงินตามสัญญา: </div>
                            </td>
                            <td style="width: 4px">&nbsp;</td>
                            <td style="width: 236px">@Html.TextBoxFor(model => model.ContractAmt, new { @readonly = "readonly", @class = "inputNumberField", style = "width: 120px;" })  BAHT</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                        </tr>
                        <tr>
                            <td>
                                <div align="right">Period: </div>
                            </td>
                            <td style="width: 4px">&nbsp;</td>
                            <td style="width: 236px">@Html.TextBoxFor(model => model.Period, new { @readonly = "readonly", style = "width: 120px;" })
                                YEAR</td>
                            <td>
                                <div align="right">Installment/Month: </div>
                            </td>
                            <td>&nbsp;</td>
                            <td>@Html.TextBoxFor(model => model.Installment, new { @class = "inputNumberField", @Value = ThaireInvestmentWeb.Util.WebUtil.FormatCurrency2(@Model.Installment), style = "width: 120px;" })  BAHT</td>
                        </tr>
                        <tr>
                            <td style="height: 4px"></td>
                            <td style="height: 4px"></td>
                            <td style="width: 236px; height: 4px;"></td>
                            <td style="height: 4px"></td>
                            <td style="height: 4px"></td>
                            <td style="height: 4px"></td>
                        </tr>
                    </table>

                    <table width="95%" border="0" align="center">
                        <tr>
                            <td style="width: 733px">
                                <div align="left">
                                    &nbsp;&nbsp; 
                                <input id="CancelBtn" type="button" name="CancelBtn" value=" Cancel " class="button">
                                </div>
                            </td>
                            <td>
                                <div align="right" style="text-align: right">
                                    &nbsp;
                                </div>
                            </td>
                        </tr>
                    </table>


                    <br />


                    <div style="width: 100%;" align="center">
                        <div style="width: 550px; border: 1px; overflow: auto;">
                            <table align="center" cellspacing="2">
                                <tr>
                                    <th colspan="3"><strong>Add New Rate </strong></th>


                                </tr>
                                <tr>
                                    <td width="212">
                                        <div align="right">MLR Rate's Date : <span class="error">*</span></div>
                                    </td>
                                    <td width="8">&nbsp;</td>
                                    <td>
                                        @Html.TextBoxFor(x => x.NewMLRRateDate, new { @value = ThaireInvestmentWeb.Util.WebUtil.FormatDate(Model.NewMLRRateDate) })
                                        @Html.ValidationMessageFor(model => model.NewMLRRateDate) (Ex.31/12/2014)
                                    </td>

                                </tr>
                                <tr>
                                    <td width="212">
                                        <div align="right">MLR Rate : <span class="error">*</span></div>
                                    </td>
                                    <td width="8">&nbsp;</td>
                                    <td>
                                        @Html.TextBoxFor(x => x.NewMLR, new { @class = "inputNumberField" })
                                        @Html.ValidationMessageFor(model => model.NewMLR)

                                    </td>

                                </tr>
                                <tr>
                                    <td width="212">
                                        <div align="right">MLR (-) : <span class="error">*</span></div>
                                    </td>
                                    <td width="8">&nbsp;</td>
                                    <td>
                                        @Html.TextBoxFor(x => x.NewMLRMinus, new { @class = "inputNumberField" })
                                        @Html.ValidationMessageFor(model => model.NewMLRMinus)

                                    </td>

                                </tr>
                                <tr>
                                    <td width="212">
                                        <div align="right">INTEREST RATE : <span class="error">*</span></div>
                                    </td>
                                    <td width="8">&nbsp;</td>
                                    <td>
                                        @Html.TextBoxFor(x => x.NewInterestRate, new { @class = "inputNumberField", @readonly = "readonly" })
                                        @Html.ValidationMessageFor(model => model.NewInterestRate)

                                    </td>

                                </tr>
                                <tr>
                                    <td width="212">
                                        <div align="right">Update Info : </div>
                                    </td>
                                    <td width="8">&nbsp;</td>
                                    <td>
                                        @Html.TextBoxFor(x => x.NewUpdateInfo)
                                        @Html.ValidationMessageFor(model => model.NewUpdateInfo)

                                    </td>

                                </tr>
                                <tr>
                                    <td colspan="3">
                                        <div align="center">

                                            <input id="SubmitBtn" type="submit" name="SubmitBtn" value=" Submit " class="button" need-confirm="true">
                                            &nbsp;&nbsp; 
                          
                                        </div>
                                    </td>
                                </tr>
                            </table>

                        </div>

                        <br />

                        @if (Model.Interest != null && Model.Interest.Count > 0)
                        {
                            var grid = new WebGrid(source: Model.Interest, ajaxUpdateContainerId: "gridselected", rowsPerPage: 25, canSort: false);
                            @grid.GetHtml(htmlAttributes: new { id = "gridselected" }, tableStyle: "tablelist", headerStyle: "headertable", displayHeader: @Model.Interest.Count > 0,
                                          columns: grid.Columns(

                                              //grid.Column(header: "NO.", format: item => (item.WebGrid.Rows.IndexOf(item) + 1) + (grid.PageIndex * grid.RowsPerPage), canSort: false),
                                              grid.Column(header: "MLR Rate's Date", format: @<text> @ThaireInvestmentWeb.Util.WebUtil.FormatDate(item.MlrDate) </text>),
                                              grid.Column(header: "MLR Rate", style: "columnNumber", format: @<text> @ThaireInvestmentWeb.Util.WebUtil.FormatDecimal2Digit(item.MlrRate) </text>),
                                              grid.Column(header: "MLR (-)", style: "columnNumber", format: @<text> @ThaireInvestmentWeb.Util.WebUtil.FormatDecimal2Digit(item.MlrMinus) </text>),
                                              grid.Column(header: "Interest Rate", style: "columnNumber", format: @<text> @ThaireInvestmentWeb.Util.WebUtil.FormatDecimal2Digit(item.InterestRate) </text>),
                                              grid.Column(header: "Update Info", format: @<text> @item.UpdateInfo </text>),

                                              grid.Column(header: "Amortize Loan Schedule", canSort: false, format: @<text>
                            <input type="button" id="BtnDelete"    name="BtnDelete"  onclick="AmortizeLoanSchedule('@item.LoansInterestDetailID    ','@item.InterestHeaderID    ')"  value="Amortize Loan"></text>)

                                              ));


                        }

                    </div>
                    <a name="grid-anchor"></a>
                    <script type="text/javascript">
                        $(function () {
                            $('th a, tfoot a').live('click', function () {
                                $(this).attr('href', '#grid-anchor');
                            });
                        });
                    </script>







                    <br>
                </td>
            </tr>
        </table>
        @Html.HiddenFor(x => x.UpdateMode)
        @Html.HiddenFor(x => x.HeaderID)
    }
</p>

